package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public List<ProductModel> findAll(ProductModel productModel) {
        String sql = "SELECT p.*,c.name cuisineName FROM product  p left join cuisine c\n" +
                "on  p.cuisine_id=c.id   where 1=1 ";
        if (!"".equals(productModel.getName())) {
            sql += " and name like '%" + productModel.getName() + "%'";
        }
        sql += " order by id desc";
        sql += " limit " + (productModel.getPage() - 1) * +productModel.getLimit() + ", " + productModel.getLimit();
        ResultSet rs = JDBC.getRs(sql);
        List<ProductModel> list = new ArrayList<>();
        try {
            while (rs.next()) {
                ProductModel model = new ProductModel();
                model.setName(rs.getString("name"));
                model.setId(rs.getInt("id"));
                model.setNormalPrice(rs.getBigDecimal("normal_price"));
                model.setImgHref(rs.getString("img_href"));
                model.setEnable(rs.getInt("enable"));
                model.setIsTop(rs.getInt("is_show"));
                model.setCuisineName(rs.getString("cuisineName"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int getCount(ProductModel productModel) {
        //        1.编写sql语句，
        String sql = "select count(*) count from product p left join cuisine c\n" +
                "on  p.cuisine_id=c.id ";
//        2.得到结果集，
        ResultSet rs = JDBC.getRs(sql);
//        3.解析结果集
        int count = 0;
        try {
            while (rs.next()) {
                count = rs.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
//        4.返回数据
        return count;
    }

    @Override
    public int add(ProductModel model) {
//        String sql = "INSERT INTO `product` \n" +
//                "(`name`, `normal_price`, `price`, `content`, \n" +
//                "`is_show`, `is_recommend`, `img_href`, `create_time`,\n" +
//                " `update_time`, `remark`, `enable`, `where_show`, `cuisine_id`) VALUES \n" +
//                " ('1', '1', '1', '1', '1', '1', '1', '111', '1', '1', '1', '1', '1')";
        String sql = "";
        return JDBC.update(sql);
    }

    @Override
    public int update(ProductModel model) {
        return 0;
    }

    @Override
    public int del(ProductModel model) {
        return 0;
    }

    @Override
    public ProductModel findById(ProductModel model) {
        return null;
    }


    public List<ProductModel> getFrontProductDetail(ProductModel model) {
        String sql = "select * from product where id=(select max(id) from product where id<" + model.getId() + " )\n" +
                "union all\n" +
                "select * from product where id=" + model.getId() +
                "union all\n" +
                "select * from product where id=(select min(id) from product where id>" + model.getId() + " )";
        List<ProductModel> list = new ArrayList<>();
        ResultSet resultSet = JDBC.getRs(sql);
        try {
            while (resultSet.next()) {
                ProductModel productModel = new ProductModel();
                productModel.setId(resultSet.getInt("id"));
                list.add(productModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
